home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / fortran / f2c_src.zip / F2C / LEX.C < prev    next >
C/C++ Source or Header  |  1991-06-10  |  31KB  |  1,454 lines

  1. /****************************************************************
  2. Copyright 1990 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "tokdefs.h"
  26. #include "p1defs.h"
  27.  
  28. #define BLANK    ' '
  29. #define MYQUOTE (2)
  30. #define SEOF 0
  31.  
  32. /* card types */
  33.  
  34. #define STEOF 1
  35. #define STINITIAL 2
  36. #define STCONTINUE 3
  37.  
  38. /* lex states */
  39.  
  40. #define NEWSTMT    1
  41. #define FIRSTTOKEN    2
  42. #define OTHERTOKEN    3
  43. #define RETEOS    4
  44.  
  45.  
  46. LOCAL int stkey;    /* Type of the current statement (DO, END, IF, etc) */
  47. extern char token[];    /* holds the actual token text */
  48. static int needwkey;
  49. ftnint yystno;
  50. flag intonly;
  51. extern int new_dcl;
  52. LOCAL long int stno;
  53. LOCAL long int nxtstno;    /* Statement label */
  54. LOCAL int parlev;    /* Parentheses level */
  55. LOCAL int parseen;
  56. LOCAL int expcom;
  57. LOCAL int expeql;
  58. LOCAL char *nextch;
  59. LOCAL char *lastch;
  60. LOCAL char *nextcd     = NULL;
  61. LOCAL char *endcd;
  62. LOCAL long prevlin;
  63. LOCAL long thislin;
  64. LOCAL int code;        /* Card type; INITIAL, CONTINUE or EOF */
  65. LOCAL int lexstate    = NEWSTMT;
  66. LOCAL char sbuf[1390];    /* Main buffer for Fortran source input.  The number
  67.                comes from lines of at most 66 characters, with at
  68.                most 20 continuation cards (or something); this is
  69.                part of the defn of the standard */
  70. LOCAL char *send    = sbuf+20*66;
  71. LOCAL int nincl    = 0;    /* Current number of include files */
  72. LOCAL long firstline;
  73. LOCAL char *laststb, *stb0;
  74. extern int addftnsrc;
  75. #define CONTMAX 100    /* max continuation lines for ! processing */
  76. char *linestart[CONTMAX];
  77. LOCAL int ncont;
  78. LOCAL char comstart[Table_size];
  79. #define USC (unsigned char *)
  80.  
  81. static char anum_buf[Table_size];
  82. #define isalnum_(x) anum_buf[x]
  83. #define isalpha_(x) (anum_buf[x] == 1)
  84.  
  85. #define COMMENT_BUF_STORE 4088
  86.  
  87. typedef struct comment_buf {
  88.     struct comment_buf *next;
  89.     char *last;
  90.     char buf[COMMENT_BUF_STORE];
  91.     } comment_buf;
  92. static comment_buf *cbfirst, *cbcur;
  93. static char *cbinit, *cbnext, *cblast;
  94. static void flush_comments();
  95. extern flag use_bs;
  96.  
  97.  
  98. /* Comment buffering data
  99.  
  100.     Comments are kept in a list until the statement before them has
  101.    been parsed.  This list is implemented with the above comment_buf
  102.    structure and the pointers cbnext and cblast.
  103.  
  104.     The comments are stored with terminating NULL, and no other
  105.    intervening space.  The last few bytes of each block are likely to
  106.    remain unused.
  107. */
  108.  
  109. /* struct Inclfile   holds the state information for each include file */
  110. struct Inclfile
  111. {
  112.     struct Inclfile *inclnext;
  113.     FILEP inclfp;
  114.     char *inclname;
  115.     int incllno;
  116.     char *incllinp;
  117.     int incllen;
  118.     int inclcode;
  119.     ftnint inclstno;
  120. };
  121.  
  122. LOCAL struct Inclfile *inclp    =  NULL;
  123. struct Keylist {
  124.     char *keyname;
  125.     int keyval;
  126.     char notinf66;
  127. };
  128. struct Punctlist {
  129.     char punchar;
  130.     int punval;
  131. };
  132. struct Fmtlist {
  133.     char fmtchar;
  134.     int fmtval;
  135. };
  136. struct Dotlist {
  137.     char *dotname;
  138.     int dotval;
  139.     };
  140. LOCAL struct Keylist *keystart[26], *keyend[26];
  141.  
  142. /* KEYWORD AND SPECIAL CHARACTER TABLES
  143. */
  144.  
  145. static struct Punctlist puncts[ ] =
  146. {
  147.     '(', SLPAR,
  148.     ')', SRPAR,
  149.     '=', SEQUALS,
  150.     ',', SCOMMA,
  151.     '+', SPLUS,
  152.     '-', SMINUS,
  153.     '*', SSTAR,
  154.     '/', SSLASH,
  155.     '$', SCURRENCY,
  156.     ':', SCOLON,
  157.     '<', SLT,
  158.     '>', SGT,
  159.     0, 0 };
  160.  
  161. LOCAL struct Dotlist  dots[ ] =
  162. {
  163.     "and.", SAND,
  164.         "or.", SOR,
  165.         "not.", SNOT,
  166.         "true.", STRUE,
  167.         "false.", SFALSE,
  168.         "eq.", SEQ,
  169.         "ne.", SNE,
  170.         "lt.", SLT,
  171.         "le.", SLE,
  172.         "gt.", SGT,
  173.         "ge.", SGE,
  174.         "neqv.", SNEQV,
  175.         "eqv.", SEQV,
  176.         0, 0 };
  177.  
  178. LOCAL struct Keylist  keys[ ] =
  179. {
  180.     { "assign",  SASSIGN  },
  181.     { "automatic",  SAUTOMATIC, YES  },
  182.     { "backspace",  SBACKSPACE  },
  183.     { "blockdata",  SBLOCK  },
  184.     { "call",  SCALL  },
  185.     { "character",  SCHARACTER, YES  },
  186.     { "close",  SCLOSE, YES  },
  187.     { "common",  SCOMMON  },
  188.     { "complex",  SCOMPLEX  },
  189.     { "continue",  SCONTINUE  },
  190.     { "data",  SDATA  },
  191.     { "dimension",  SDIMENSION  },
  192.     { "doubleprecision",  SDOUBLE  },
  193.     { "doublecomplex", SDCOMPLEX, YES  },
  194.     { "elseif",  SELSEIF, YES  },
  195.     { "else",  SELSE, YES  },
  196.     { "endfile",  SENDFILE  },
  197.     { "endif",  SENDIF, YES  },
  198.     { "enddo", SENDDO, YES },
  199.     { "end",  SEND  },
  200.     { "entry",  SENTRY, YES  },
  201.     { "equivalence",  SEQUIV  },
  202.     { "external",  SEXTERNAL  },
  203.     { "format",  SFORMAT  },
  204.     { "function",  SFUNCTION  },
  205.     { "goto",  SGOTO  },
  206.     { "implicit",  SIMPLICIT, YES  },
  207.     { "include",  SINCLUDE, YES  },
  208.     { "inquire",  SINQUIRE, YES  },
  209.     { "intrinsic",  SINTRINSIC, YES  },
  210.     { "integer",  SINTEGER  },
  211.     { "logical",  SLOGICAL  },
  212.     { "namelist", SNAMELIST, YES },
  213.     { "none", SUNDEFINED, YES },
  214.     { "open",  SOPEN, YES  },
  215.     { "parameter",  SPARAM, YES  },
  216.     { "pause",  SPAUSE  },
  217.     { "print",  SPRINT  },
  218.     { "program",  SPROGRAM, YES  },
  219.     { "punch",  SPUNCH, YES  },
  220.     { "read",  SREAD  },
  221.     { "real",  SREAL  },
  222.     { "return",  SRETURN  },
  223.     { "rewind",  SREWIND  },
  224.     { "save",  SSAVE, YES  },
  225.     { "static",  SSTATIC, YES  },
  226.     { "stop",  SSTOP  },
  227.     { "subroutine",  SSUBROUTINE  },
  228.     { "then",  STHEN, YES  },
  229.     { "undefined", SUNDEFINED, YES  },
  230.     { "while", SWHILE, YES  },
  231.     { "write",  SWRITE  },
  232.     { 0, 0 }
  233. };
  234.  
  235. LOCAL void analyz(), crunch(), store_comment();
  236. LOCAL int getcd(), getcds(), getkwd(), gettok();
  237. LOCAL char *stbuf[3];
  238.  
  239. inilex(name)
  240. char *name;
  241. {
  242.     stbuf[0] = Alloc(3*P1_STMTBUFSIZE);
  243.     stbuf[1] = stbuf[0] + P1_STMTBUFSIZE;
  244.     stbuf[2] = stbuf[1] + P1_STMTBUFSIZE;
  245.     nincl = 0;
  246.     inclp = NULL;
  247.     doinclude(name);
  248.     lexstate = NEWSTMT;
  249.     return(NO);
  250. }
  251.  
  252.  
  253.  
  254. /* throw away the rest of the current line */
  255. flline()
  256. {
  257.     lexstate = RETEOS;
  258. }
  259.  
  260.  
  261.  
  262. char *lexline(n)
  263. int *n;
  264. {
  265.     *n = (lastch - nextch) + 1;
  266.     return(nextch);
  267. }
  268.  
  269.  
  270.  
  271.  
  272.  
  273. doinclude(name)
  274. char *name;
  275. {
  276.     FILEP fp;
  277.     struct Inclfile *t;
  278.  
  279.     if(inclp)
  280.     {
  281.         inclp->incllno = thislin;
  282.         inclp->inclcode = code;
  283.         inclp->inclstno = nxtstno;
  284.         if(nextcd)
  285.             inclp->incllinp = copyn(inclp->incllen = endcd-nextcd , nextcd);
  286.         else
  287.             inclp->incllinp = 0;
  288.     }
  289.     nextcd = NULL;
  290.  
  291.     if(++nincl >= MAXINCLUDES)
  292.         Fatal("includes nested too deep");
  293.     if(name[0] == '\0')
  294.         fp = stdin;
  295.     else
  296.         fp = fopen(name, textread);
  297.     if (fp)
  298.     {
  299.         t = inclp;
  300.         inclp = ALLOC(Inclfile);
  301.         inclp->inclnext = t;
  302.         prevlin = thislin = 0;
  303.         infname = inclp->inclname = name;
  304.         infile = inclp->inclfp = fp;
  305.     }
  306.     else
  307.     {
  308.         fprintf(diagfile, "Cannot open file %s\n", name);
  309.         done(1);
  310.     }
  311. }
  312.  
  313.  
  314.  
  315.  
  316. LOCAL popinclude()
  317. {
  318.     struct Inclfile *t;
  319.     register char *p;
  320.     register int k;
  321.  
  322.     if(infile != stdin)
  323.         clf(&infile, infname, 1);    /* Close the input file */
  324.     free(infname);
  325.  
  326.     --nincl;
  327.     t = inclp->inclnext;
  328.     free( (charptr) inclp);
  329.     inclp = t;
  330.     if(inclp == NULL) {
  331.         infname = 0;
  332.         return(NO);
  333.         }
  334.  
  335.     infile = inclp->inclfp;
  336.     infname = inclp->inclname;
  337.     prevlin = thislin = inclp->incllno;
  338.     code = inclp->inclcode;
  339.     stno = nxtstno = inclp->inclstno;
  340.     if(inclp->incllinp)
  341.     {
  342.         endcd = nextcd = sbuf;
  343.         k = inclp->incllen;
  344.         p = inclp->incllinp;
  345.         while(--k >= 0)
  346.             *endcd++ = *p++;
  347.         free( (charptr) (inclp->incllinp) );
  348.     }
  349.     else
  350.         nextcd = NULL;
  351.     return(YES);
  352. }
  353.  
  354.  static void